package theOrbalEra.common.block;

import java.util.Random;

import theOrbalEra.common.TheOrbalEra;
import theOrbalEra.common.util.DefultValue;
import theOrbalEra.common.util.idList.OrbalGuiID;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class BlockOrbalFactory extends OrbalBlockContainer {
	private Random Ran = new Random();
    public Icon front;
    public Icon down;
    public Icon up;
	public BlockOrbalFactory(int par1) {
		super(par1, Material.iron);
		
		
	}

	@Override
	public TileEntity createNewTileEntity(World var1) {
		
		return new TileEntityOrballFactory();
	}
	
	
	
	public boolean onBlockActivated(World par1World, int par2, int par3,int par4, EntityPlayer par5EntityPlayer, int par6, float par7,
			float par8, float par9) {
		if (par1World.isRemote) {
			return true;
		} else {
			TileEntityOrballFactory var10 = (TileEntityOrballFactory) par1World.getBlockTileEntity(par2, par3, par4);

			if (var10 != null) {
				par5EntityPlayer.openGui(TheOrbalEra.instance, OrbalGuiID.Orbal_FAC, par1World, par2,
						par3, par4);
			}

			return true;
		}
	}
	@Override
	public void breakBlock(World par1World, int par2, int par3, int par4,
			int par5, int par6) {
		
		TileEntityOrballFactory  var7 = (TileEntityOrballFactory ) par1World.getBlockTileEntity(par2, par3, par4);

			if (var7 != null) {
				
					ItemStack var9 = var7.getStackInSlot(0);

					if (var9 != null) {
						float var10 = this.Ran.nextFloat() * 0.8F + 0.1F;
						float var11 = this.Ran.nextFloat() * 0.8F + 0.1F;
						float var12 = this.Ran.nextFloat() * 0.8F + 0.1F;

						while (var9.stackSize > 0) {
							int var13 = this.Ran.nextInt(21) + 10;

							if (var13 > var9.stackSize) {
								var13 = var9.stackSize;
							}

							var9.stackSize -= var13;
							EntityItem var14 = new EntityItem(par1World,
									(double) ((float) par2 + var10),
									(double) ((float) par3 + var11),
									(double) ((float) par4 + var12),
									new ItemStack(var9.itemID, var13,
											var9.getItemDamage()));

							

							float var15 = 0.05F;
							var14.motionX = (double) ((float) this.Ran
									.nextGaussian() * var15);
							var14.motionY = (double) ((float) this.Ran
									.nextGaussian() * var15 + 0.2F);
							var14.motionZ = (double) ((float) this.Ran
									.nextGaussian() * var15);
							par1World.spawnEntityInWorld(var14);
						}
					}
				}
			
		

		super.breakBlock(par1World, par2, par3, par4, par5, par6);
	}
	
	@Override
	 public Icon getIcon(int par1, int par2)
	    {
	        if(par1==1)
	        	return this.up;
	        else if(par1==0)
	        	return this.down;
	        else if(par1==3)
	        	return this.front;
	        else
	        	return this.blockIcon;
	    }
	 
	    
	    
	    @Override
	@SideOnly(Side.CLIENT)
	public void registerIcons(IconRegister par1IconRegister) {
		this.front=par1IconRegister.registerIcon(DefultValue.ModName+":OrballFactoryF");
		this.blockIcon=par1IconRegister.registerIcon(DefultValue.ModName+":OrballFactoryN");
		this.up=par1IconRegister.registerIcon(DefultValue.ModName+":OrballFactoryU");
		this.down=par1IconRegister.registerIcon(DefultValue.ModName+":OrballFactoryD");
	}
@Override
		public Icon getBlockTexture(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
	    {
	        if (par5 == 1)
	        {
	            return this.up;
	        }
	        else if(par5 == 0){
			return this.down;
			}
			else
	        {
	            int var6 = par1IBlockAccess.getBlockMetadata(par2, par3, par4);
	            return par5 != var6 ? this.blockIcon : this.front;
	        }
	    }
	    @Override
		public void onBlockAdded(World par1World, int par2, int par3, int par4)
	    {
	        super.onBlockAdded(par1World, par2, par3, par4);
	        this.setDefaultDirection(par1World, par2, par3, par4);
	    }
		 private void setDefaultDirection(World par1World, int par2, int par3, int par4)
		    {
		        if (!par1World.isRemote)
		        {
		            int var5 = par1World.getBlockId(par2, par3, par4 - 1);
		            int var6 = par1World.getBlockId(par2, par3, par4 + 1);
		            int var7 = par1World.getBlockId(par2 - 1, par3, par4);
		            int var8 = par1World.getBlockId(par2 + 1, par3, par4);
		            byte var9 = 3;

		            if (Block.opaqueCubeLookup[var5] && !Block.opaqueCubeLookup[var6])
		            {
		                var9 = 3;
		            }

		            if (Block.opaqueCubeLookup[var6] && !Block.opaqueCubeLookup[var5])
		            {
		                var9 = 2;
		            }

		            if (Block.opaqueCubeLookup[var7] && !Block.opaqueCubeLookup[var8])
		            {
		                var9 = 5;
		            }

		            if (Block.opaqueCubeLookup[var8] && !Block.opaqueCubeLookup[var7])
		            {
		                var9 = 4;
		            }

		            par1World.setBlockMetadataWithNotify(par2, par3, par4, var9,2);
		        }
		    }
		 public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving)
		    {
		        int var6 = MathHelper.floor_double((double)(par5EntityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;

		        if (var6 == 0)
		        {
		            par1World.setBlockMetadataWithNotify(par2, par3, par4, 2,2);
		        }

		        if (var6 == 1)
		        {
		            par1World.setBlockMetadataWithNotify(par2, par3, par4, 5,2);
		        }

		        if (var6 == 2)
		        {
		            par1World.setBlockMetadataWithNotify(par2, par3, par4, 3,2);
		        }

		        if (var6 == 3)
		        {
		            par1World.setBlockMetadataWithNotify(par2, par3, par4, 4,2);
		        }
		    }
		
	    
	  


}
